//
//  FQLogManager.m
//  FQLog_Example
//
//  Created by ffq on 2023/3/13.
//  Copyright © 2023 feng. All rights reserved.
//

#import "FQLogManager.h"

#import "FQOsFormatter.h"

#import "FQFileFormatter.h"
#import "FQFileLogManager.h"

#import "FQReadFileListVC.h"

//设置默认的log等级
#ifdef DEBUG
DDLogLevel ddLogLevel = DDLogLevelDebug;
#else
DDLogLevel ddLogLevel = DDLogLevelWarning;
#endif

@interface FQLogManager()

/** 控制台日志 */
@property (nonatomic, strong) DDOSLogger *osLogger;

/** 文件写入日志 */
@property (nonatomic, strong) DDFileLogger *fileLogger;

@end

@implementation FQLogManager

+ (instancetype)sharedInstance {
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

// 启动
+ (void)start {
    [[FQLogManager sharedInstance]addOsLogger];
    [[FQLogManager sharedInstance]addFileLogger];
}

// 显示日志目录
+ (void)showLogVC {
    FQReadFileListVC *vc = [[FQReadFileListVC alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    
//    // ipa用UIModalPresentationFormSheet 会出现显示异常，所以取消掉
//    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//        nav.modalPresentationStyle = UIModalPresentationFormSheet;
//    }else {
//        nav.modalPresentationStyle = UIModalPresentationFullScreen;
//    }
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
}


#pragma mark - Logger添加删除

- (void)addOsLogger {
    [DDLog addLogger:self.osLogger];
}

- (void)addFileLogger {
    [DDLog addLogger:self.fileLogger];
}

- (void)removeOsLogger {
    [DDLog removeLogger:self.osLogger];
}

- (void)removeFileLogger {
    [DDLog removeLogger:self.fileLogger];
}

#pragma mark - 等级切换
- (void)switchLogLevel: (DDLogLevel) logLevel {
    ddLogLevel = logLevel;
}

#pragma mark - 文件Log操作

- (void)createAndRollToNewFile {
    //achive现在的文件，新生成一个文件，并将以后的log写入新文件
    [_fileLogger rollLogFileWithCompletionBlock:^{
        NSLog(@"rollLogFileWithCompletionBlock");
    }];
}

// 文件存储的默认路径
- (NSString *)defaultLogsDirectory {
    return _fileLogger.logFileManager.logsDirectory;
}

//所有log文件路径
- (NSArray *)filePaths {
    return _fileLogger.logFileManager.sortedLogFilePaths;
}

//当前活跃的log文件路径
- (NSString *)currentFilePath {
    
    NSString *filePath = _fileLogger.currentLogFileInfo.filePath;
    return filePath;
}

#pragma mark - getter/setter

- (DDOSLogger *)osLogger {
    if(!_osLogger) {
        _osLogger = [DDOSLogger sharedInstance];
        _osLogger.logFormatter = [[FQOsFormatter alloc]init];
    }
    return _osLogger;
}

-(DDFileLogger *)fileLogger {
    if (!_fileLogger) {
        FQFileLogManager *fileManager = [[FQFileLogManager alloc]init];
        _fileLogger = [[DDFileLogger alloc]initWithLogFileManager:fileManager];
        _fileLogger.logFormatter = [[FQFileFormatter alloc]init];
        
        //重用log文件，不要每次启动都创建新的log文件(默认值是NO)
        _fileLogger.doNotReuseLogFiles = NO;
        //log文件在24小时内有效，超过时间创建新log文件(默认值是24小时)
        _fileLogger.rollingFrequency = 60*60*24;
        //log文件的最大10M(默认值1M)
        _fileLogger.maximumFileSize = 1023*1024*1;
        //最多保存20个log文件(默认值是5)
        _fileLogger.logFileManager.maximumNumberOfLogFiles = 100;
        //log文件夹最多保存100M(默认值是20M)
        _fileLogger.logFileManager.logFilesDiskQuota = 1024*1024*100;
    }
    return _fileLogger;
}

@end
